package com.sky.controller.user;

import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.web.bind.annotation.*;

/**
 * 用户订单相关接口
 */
@RequestMapping("/user/order")
@RestController("userOrderController")
@Api(tags = "用户订单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 提交订单接口
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("提交订单接口")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("提交订单:{}",ordersSubmitDTO);
        return Result.success(orderService.submit(ordersSubmitDTO));
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO 主要包含订单号（由下单接口生成）
     * @return 支付参数，交由前端吊起微信支付
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 查询历史订单
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单接口")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {//如果要把请求参数直接放入实体类，则不能加RequestParam注解
        log.info("查询历史订单:{}",ordersPageQueryDTO);
        return Result.success(orderService.historyOrders(ordersPageQueryDTO));
    }

    /**
     * 查询订单详情
     * @param id 订单id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> orderDetail(@PathVariable Long id) {
        log.info("查询订单详情:{}",id);
        return Result.success(orderService.orderDetail(id));
    }

    /**
     * 取消订单
     * @param id 订单id
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable Long id) throws Exception {
        log.info("取消订单:{}",id);
        orderService.cancelByUser(id);
        return Result.success();
    }

    /**
     * 再来一单功能（将对应订单商品再次加入购物车中）
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id) {
        log.info("再来一单，订单号：{}",id);
        orderService.repetition(id);
        return Result.success();
    }

    /**
     * 客户催单接口
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("客户催单接口")
    public Result reminder(@PathVariable Long id) {
        log.info("客户催单：{}",id);
        orderService.reminder(id);
        return Result.success();
    }
}
